package com.ruoyi.school.event;

import com.ruoyi.school.SchoolService;
import com.ruoyi.system.service.ISysConfigService;
import org.openapi.common.ApiException;
import org.openapi.event.EventService;
import org.openapi.service.IDbService;
import org.openapi.utils.StrUtil;
import org.openapi.vo.TableQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 新增学生请假信息前，查询出学期、当前用户所属院系、专业班级等
 */
@Component
public class SelectCourseEvent implements EventService {
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private IDbService dbService;
    @Autowired
    private ISysConfigService configService;
    @Override
    public void process(Object data) {
        TableQuery tableQuery = new TableQuery();

        //学生信息，兼容导入
        Map<String,Object> selectData = (Map<String,Object>)data;
        if(!selectData.containsKey("student_id") || selectData.get("student_id") == null){
            if(selectData.containsKey("student_num") && selectData.get("student_num") != null){
                tableQuery.setTable("base_student");
                tableQuery.addData("num", selectData.get("student_num"));
                Map stuData = dbService.find(tableQuery);
                if(stuData != null) {
                    selectData.put("student_id", stuData.get("id"));
                    selectData.put("student_name", stuData.get("name"));
                }
                tableQuery.getData().remove("student_num");
            }else{
                Map userInfo = schoolService.getUserInfo();
                if(userInfo == null || !"student".equalsIgnoreCase(userInfo.get("_identity").toString())){
                    throw new ApiException("您不是学生");
                }
                selectData.put("student_id", userInfo.get("id"));
                selectData.put("student_name", userInfo.get("name"));
                selectData.put("student_num", userInfo.get("num"));
            }
        }else if("0".equalsIgnoreCase(selectData.get("student_id").toString())){
            selectData.remove("student_id");
            Map userInfo = schoolService.getUserInfo();
            if(userInfo == null || !"student".equalsIgnoreCase(userInfo.get("_identity").toString())){
                throw new ApiException("您不是学生");
            }
            selectData.put("student_id", userInfo.get("id"));
            selectData.put("student_name", userInfo.get("name"));
            selectData.put("student_num", userInfo.get("num"));
        }

        if(!selectData.containsKey("student_id") || selectData.get("student_id") == null){
            throw new ApiException("学生信息不存在");
        }

        //查询课程信息，兼容导入
        Map courseData = null;
        tableQuery.setTable("teach_course");
        tableQuery.getData().remove("id");
        if(!selectData.containsKey("tc_id") || selectData.get("tc_id") == null){
            if(selectData.containsKey("tc_num") && selectData.get("tc_num") != null){
                tableQuery.addData("num", selectData.get("tc_num"));
                courseData = dbService.find(tableQuery);
                tableQuery.getData().remove("num");
            }
        }else{
            tableQuery.setTable("teach_course");
            tableQuery.addData("id", selectData.get("tc_id"));
            courseData = dbService.find(tableQuery);
            tableQuery.getData().remove("id");
        }
        if(courseData != null){
            selectData.put("sem_id", courseData.get("sem_id"));
            selectData.put("course_id", courseData.get("course_id"));
            selectData.put("tc_id", courseData.get("id"));
            selectData.put("tc_num", courseData.get("num"));
        }else{
            throw new ApiException("必须选择课程");
        }

        //判断是否在其它学期选择过同一门课并审核通过，或者本学期已经选择过相同课程
        String sql = "SELECT count(cs.id) val FROM teach_student cs join teach_course t on cs.tc_id = t.id where cs.student_id=";
        sql += selectData.get("student_id") +" and t.course_id="+selectData.get("course_id")+" and ( (t.sem_id!="+ selectData.get("sem_id")+ " AND cs.check_status ='2') OR t.sem_id ="+ selectData.get("sem_id")+")";
        List<Map<String,Object>> list = dbService.query(sql);
        int total = Integer.parseInt(list.get(0).get("val").toString());
        if(total > 0){
            throw new ApiException("已选择过同类课程");
        }

        //判断本次学期选择课程数是否超出限制
        list = dbService.query("SELECT count(cs.id) val FROM teach_student cs join teach_course t on cs.tc_id = t.id WHERE t.sem_id = " + selectData.get("sem_id").toString() + " AND cs.student_id = " + selectData.get("student_id").toString());
        total = Integer.parseInt(list.get(0).get("val").toString());
        String maxStr = configService.selectConfigByKey("teach.select.max");
        int maxLimit = StrUtil.isEmpty(maxStr) ? 3 : Integer.parseInt(maxStr);
        if(total >= maxLimit){
            throw new ApiException("每学期最多可选择"+maxLimit+"门课程");
        }

        //填充课程信息
        tableQuery.setTable("base_course");
        tableQuery.addData("id", selectData.get("course_id"));
        Map bcData = dbService.find(tableQuery);
        selectData.put("course_name", bcData.get("name"));

        //填充教师
        tableQuery.setTable("base_teacher");
        tableQuery.addData("id", courseData.get("teacher_id"));
        bcData = dbService.find(tableQuery);
        selectData.put("teacher_name", bcData.get("name"));
    }
}
